package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentStandardValueDAO;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentStandardValue;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-19
 * 类描述：EnvironmentStandardValue类DAO实现
 */
public class EnvironmentStandardValueDAOImpl implements EnvironmentStandardValueDAO
{

    @Override
    public boolean save(EnvironmentStandardValue esv)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(esv);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(EnvironmentStandardValue esv)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(esv);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(EnvironmentStandardValue esv)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(esv);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnvironmentStandardValue getById(int id)
    {
        EnvironmentStandardValue result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (EnvironmentStandardValue) s.get(EnvironmentStandardValue.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnvironmentStandardValue get(int enterpriseID, int environmentFactorID)
    {
        EnvironmentStandardValue result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnvironmentStandardValue "
                                     + "where enterprise.id=" + enterpriseID
                                     + " and factor.id=" + environmentFactorID);
            result = (EnvironmentStandardValue) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public EnvironmentStandardValue get(int enterpriseID, String environmentFactorName)
    {
        EnvironmentStandardValue result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from EnvironmentStandardValue "
                                     + "where enterprise.id=" + enterpriseID
                                     + " and factor.name='" + environmentFactorName + "'");
            result = (EnvironmentStandardValue) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
